module common.lightfog;

import std.stdio, std.datetime, std.math, std.algorithm;
import mirdefine;

import allegro5.allegro;
import allegro5.allegro_primitives;


// Helper routine
private int iround(real x)
{
    return cast(int)lrint(x);
}


public class LightFog
{
    protected ALLEGRO_VERTEX m_avLight[(NUM_CNTY_LIGHTTILE+1)][(NUM_CNTX_LIGHTTILE+1)];
    protected ALLEGRO_VERTEX m_avSaveLight[(NUM_CNTY_LIGHTTILE+1)][(NUM_CNTX_LIGHTTILE+1)];
    protected int m_pwLightIndices[NUM_LIGHTTILE_INDICES];
    protected ALLEGRO_COLOR m_dwLightColor; 
    
    
    public this()
    {
        Init(al_map_rgb(220, 220, 220));
    }
    
    public void Init(ALLEGRO_COLOR color)
    {
        int nCntX, nCntY;
        
        m_dwLightColor = color;
        
        for ( nCntY = 0; nCntY < (NUM_CNTY_LIGHTTILE+1); nCntY++ )
        {
            for ( nCntX = 0; nCntX < (NUM_CNTX_LIGHTTILE+1); nCntX++ )
            {
                float fXPos = (TILE_START_XPOS+(nCntX*TILE_WIDTH));
                float fYPos = (TILE_START_YPOS+(nCntY*TILE_HEIGHT));
                float fu = 0;
                float fv = 0;
                
                m_avLight[nCntY][nCntX] = ALLEGRO_VERTEX(fXPos, fYPos, 0f,
                                                         fu, fv,
                                                         m_dwLightColor);
            }
        }
        
        m_avSaveLight[] = m_avLight[];
        
        // Å¸ÀÏÆú¸®°ïÀÇ ÀÎµ¦½º »ý¼º.
        auto pwIndex = &m_pwLightIndices[0];
        
        for ( nCntY = 0; nCntY < NUM_CNTY_LIGHTTILE; nCntY++ )
        {
            for ( nCntX = 0; nCntX < NUM_CNTX_LIGHTTILE; nCntX++ )
            {
                *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY));  
                *pwIndex++ = (nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));  
                *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));
                *pwIndex++ = (nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));  
                *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY));  
                *pwIndex++ = ((nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY));
            }
        }
    }
    
    public void ShowLightFog()
    {
        int op, source, dest;
        al_get_blender(&op, &source, &dest);
        al_set_blender(ALLEGRO_BLEND_OPERATIONS.ALLEGRO_ADD, ALLEGRO_BLEND_MODE.ALLEGRO_ZERO, ALLEGRO_BLEND_MODE.ALLEGRO_SRC_COLOR);

        auto prim = al_draw_indexed_prim(m_avLight.ptr, null, null, m_pwLightIndices.ptr,
                             NUM_LIGHTTILE_INDICES, ALLEGRO_PRIM_TYPE.ALLEGRO_PRIM_TRIANGLE_LIST);

        al_set_blender(op, source, dest);
    }
    
    // »õ·Î ¹Ù²ï »öÀ» m_avSaveLight¿¡ ÀúÀå½ÃÅ²´Ù.
    public void ChangeLightColor(ALLEGRO_COLOR color)
    {
        int nCntX, nCntY;
        
        if ( m_dwLightColor != color )
        {
            for ( nCntY = 0; nCntY < NUM_CNTY_LIGHTTILE+1; nCntY++ )
            {
                for ( nCntX = 0; nCntX < NUM_CNTX_LIGHTTILE+1; nCntX++ )
                {
                    m_avLight[nCntY][nCntX].color = color;
                }
            }
            m_dwLightColor = color;
            m_avSaveLight[] = m_avLight[];
        }
    }
    
    public void ClearSavedLightColor()
    {
        m_avLight[] = m_avSaveLight[];
    }
    
    public bool GetScreenPosToLightTile(int nX, int nY, ref int nTileX, ref int nTileY)
    {
        nTileX = (nX - TILE_START_XPOS) / TILE_WIDTH;
        nTileY = (nY - TILE_START_YPOS) / TILE_HEIGHT;
        
        return true;
    }
    
    public bool SetLightRadiusWithRing(int XLightPos, int YLightPos, int nSmallRadius, 
                                       int nSmallRed, int nSmallGreen, int nSmallBlue, 
                                       int nLargeRadius,
                                       int nLargeRed, int nLargeGreen, int nLargeBlue)
    {
        int nX, nY;
        GetScreenPosToLightTile(XLightPos, YLightPos, nX, nY);
        
        int nStartTileX         = nX - nLargeRadius;
        int nStartTileY         = nY - nLargeRadius;
        int nEndTileX           = nX + nLargeRadius + 1;
        int nEndTileY           = nY + nLargeRadius + 1;
        
        // y¸¦ ÃàÀ¸·Î ÇØ¼­ Á© ¾îµÎ¿î ºÎºÐÀ» ±¸ÇÑ´Ù.(xº¸´Ù y°¡ ´õ ÀûÀ¸¹Ç·Î)
        nLargeRadius = (nLargeRadius * TILE_HEIGHT);
        nSmallRadius = (nSmallRadius * TILE_HEIGHT);
        
        real rLengthCnt         = 0f;
        int nSelectedRedValue   = 0; 
        int nSelectedGreenValue = 0;
        int nSelectedBlueValue  = 0;
        
        ubyte rr, gg, bb, aa;

        if ( nStartTileX < 0 )                  nStartTileX = 0;
        if ( nStartTileY < 0 )                  nStartTileY = 0;
        if ( nEndTileX > NUM_CNTX_LIGHTTILE+1 ) nEndTileX = NUM_CNTX_LIGHTTILE+1;
        if ( nEndTileY > NUM_CNTY_LIGHTTILE+1 ) nEndTileY = NUM_CNTY_LIGHTTILE+1;   
        
        for ( int nCntY = nStartTileY; nCntY < nEndTileY; nCntY++ )
        {
            for ( int nCntX = nStartTileX; nCntX < nEndTileX; nCntX++ )
            {           
                if ( nCntX < NUM_CNTX_LIGHTTILE && nCntY < NUM_CNTY_LIGHTTILE )
                {
                    auto pwIndex = &m_pwLightIndices[(nCntX+nCntY*(NUM_CNTX_LIGHTTILE))*6];

                    // Æ÷ÀÎÆ®Á¡À» Æ÷ÇÔÇÏÁö ¾Ê´Â ÁÂÇÏ. Æ÷ÀÎÆ®Á¡À» Æ÷ÇÔÇÏÁö ¾Ê´Â ¿ì»ó.
                    if ( (nCntX > nX && nCntY < nY) || (nCntX < nX && nCntY > nY) )
                    {
                        *pwIndex++ = (nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY);
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY);  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));
                        *pwIndex++ = (nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY);
                        *pwIndex++ = nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));
                    }
                    else
                    {
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY);  
                        *pwIndex++ = nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));
                        *pwIndex++ = nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY);  
                        *pwIndex++ = (nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY);
                    }
                }

                real xx = XLightPos-(nCntX*TILE_WIDTH+TILE_START_XPOS);
                real yy = YLightPos-(nCntY*TILE_HEIGHT+TILE_START_YPOS);
                rLengthCnt = sqrt(xx * xx + yy * yy);

                if (rLengthCnt < nSmallRadius)
                {
                    nSelectedRedValue   = nSmallRed - iround((nSmallRed * rLengthCnt) / nLargeRadius);
                    nSelectedGreenValue = nSmallGreen - iround((nSmallGreen * rLengthCnt) / nLargeRadius);
                    nSelectedBlueValue  = nSmallBlue - iround((nSmallBlue * rLengthCnt) / nLargeRadius);
                }
                else if ((rLengthCnt >= nSmallRadius) && (rLengthCnt <= nLargeRadius))
                {
                    nSelectedRedValue   = nLargeRed - iround((nLargeRed * rLengthCnt) / nLargeRadius);
                    nSelectedGreenValue = nLargeGreen - iround((nLargeGreen * rLengthCnt) / nLargeRadius);
                    nSelectedBlueValue  = nLargeBlue - iround((nLargeBlue * rLengthCnt) / nLargeRadius);
                }
                else
                {
                    nSelectedRedValue = nSelectedGreenValue = nSelectedBlueValue = 0;
                }

                if ( nSelectedRedValue   < 0 ) nSelectedRedValue = 0;
                if ( nSelectedGreenValue < 0 ) nSelectedGreenValue = 0;
                if ( nSelectedBlueValue  < 0 ) nSelectedBlueValue = 0;
                
                al_unmap_rgba(m_avLight[nCntY][nCntX].color, &rr, &gg, &bb, &aa);
             
                if ( rr == 0 && gg == 0 && bb == 0)
                    m_avLight[nCntY][nCntX].color = al_map_rgb(cast(ubyte)nSelectedRedValue, cast(ubyte)nSelectedGreenValue, cast(ubyte)nSelectedBlueValue); 
                else
                {
                    nSelectedRedValue += rr;
                    nSelectedGreenValue += gg;
                    nSelectedBlueValue += bb;

                    if ( nSelectedRedValue   > 255 ) nSelectedRedValue  = 255;
                    if ( nSelectedGreenValue > 255 ) nSelectedGreenValue = 255;
                    if ( nSelectedBlueValue  > 255 ) nSelectedBlueValue = 255;

                    m_avLight[nCntY][nCntX].color = al_map_rgb(cast(ubyte)nSelectedRedValue, cast(ubyte)nSelectedGreenValue, cast(ubyte)nSelectedBlueValue);
                }
            }
        }
        
        return true;
    }
    
    public bool SetLightRadiusWithCircle(int XLightPos, int YLightPos, int nRadius,
                                         int nRedValue, int nGreenValue, int nBlueValue, 
                                         float rDarkRate = 1.0f)
    {
        int nX, nY, nCntX, nCntY;
        GetScreenPosToLightTile(XLightPos, YLightPos, nX, nY);
        
        int nStartTileX         = nX - nRadius;
        int nStartTileY         = nY - nRadius;
        int nEndTileX           = nX + nRadius + 1;
        int nEndTileY           = nY + nRadius + 1;
        
        // y¸¦ ÃàÀ¸·Î ÇØ¼­ Á© ¾îµÎ¿î ºÎºÐÀ» ±¸ÇÑ´Ù.(xº¸´Ù y°¡ ´õ ÀûÀ¸¹Ç·Î)
        nRadius = (nRadius * TILE_HEIGHT);

        
        real rLengthCnt         = 0f;
        int nSelectedRedValue   = 0; 
        int nSelectedGreenValue = 0;
        int nSelectedBlueValue  = 0;
        
        ubyte rr, gg, bb, aa;

        if ( nStartTileX < 0 ) nStartTileX = 0;
        if ( nStartTileY < 0 ) nStartTileY = 0;
        if ( nEndTileX > NUM_CNTX_LIGHTTILE+1 ) nEndTileX = NUM_CNTX_LIGHTTILE+1;
        if ( nEndTileY > NUM_CNTY_LIGHTTILE+1 ) nEndTileY = NUM_CNTY_LIGHTTILE+1;   
        
        
        for ( nCntY = nStartTileY; nCntY < nEndTileY; nCntY++ )
        {
            for ( nCntX = nStartTileX; nCntX < nEndTileX; nCntX++ )
            {
                if ( nCntX < NUM_CNTX_LIGHTTILE && nCntY < NUM_CNTY_LIGHTTILE )
                {
                    auto pwIndex = &m_pwLightIndices[(nCntX+nCntY*(NUM_CNTX_LIGHTTILE))*6];
                    // Æ÷ÀÎÆ®Á¡À» Æ÷ÇÔÇÏÁö ¾Ê´Â ÁÂÇÏ. Æ÷ÀÎÆ®Á¡À» Æ÷ÇÔÇÏÁö ¾Ê´Â ¿ì»ó.
                    if ( (nCntX > nX && nCntY < nY) || (nCntX < nX && nCntY > nY) )
                    {
                        *pwIndex++ = ((nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY));
                        *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY));  
                        *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));
                        *pwIndex++ = ((nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY));
                        *pwIndex++ = (nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));  
                        *pwIndex = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));
                    }
                    else
                    {
                        *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY));  
                        *pwIndex++ = (nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));  
                        *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));
                        *pwIndex++ = (nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1)));  
                        *pwIndex++ = ((nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY));  
                        *pwIndex = ((nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY));
                    }
                }

                real xx = XLightPos-(nCntX*TILE_WIDTH+TILE_START_XPOS);
                real yy = YLightPos-(nCntY*TILE_HEIGHT+TILE_START_YPOS);
                rLengthCnt = sqrt(xx * xx + yy * yy);

                /*          RECT rc;
                SetRect(&rc,
                (nCntX*TILE_WIDTH+TILE_START_XPOS),
                (nCntY*TILE_HEIGHT+TILE_START_YPOS),
                (nCntX*TILE_WIDTH+TILE_START_XPOS+TILE_WIDTH),
                (nCntY*TILE_HEIGHT+TILE_START_YPOS+TILE_HEIGHT));
                g_xMainWnd.DrawWithGDI(rc, NULL, RGB(0, 255, 255), 1);
                */
                if (rLengthCnt <= nRadius)
                {
                    nSelectedRedValue   = nRedValue - iround((nRedValue * rLengthCnt * rDarkRate) / nRadius);
                    nSelectedGreenValue = nGreenValue - iround((nGreenValue * rLengthCnt * rDarkRate) / nRadius);
                    nSelectedBlueValue  = nBlueValue - iround((nBlueValue * rLengthCnt * rDarkRate) / nRadius);
                    /*              SetRect(&rc,
                    (nCntX*TILE_WIDTH+TILE_START_XPOS),
                    (nCntY*TILE_HEIGHT+TILE_START_YPOS),
                    (nCntX*TILE_WIDTH+TILE_START_XPOS+2),
                    (nCntY*TILE_HEIGHT+TILE_START_YPOS+2));
                    g_xMainWnd.DrawWithGDI(rc, NULL, RGB(255, 0, 0), 1);
                */          
                }
                else
                {
                    nSelectedRedValue = nSelectedGreenValue = nSelectedBlueValue = 0;
                }
                
                if ( nSelectedRedValue   < 0 ) nSelectedRedValue = 0;
                if ( nSelectedGreenValue < 0 ) nSelectedGreenValue = 0;
                if ( nSelectedBlueValue  < 0 ) nSelectedBlueValue = 0;
                
                al_unmap_rgba(m_avLight[nCntY][nCntX].color, &rr, &gg, &bb, &aa);
                
                if ( rr == 0 && gg == 0 && bb == 0 )
                    m_avLight[nCntY][nCntX].color = al_map_rgb(cast(ubyte)nSelectedRedValue, cast(ubyte)nSelectedGreenValue, cast(ubyte)nSelectedBlueValue);
                else
                {
                    nSelectedRedValue += rr;
                    nSelectedGreenValue += gg;
                    nSelectedBlueValue += bb;
                    
                    if ( nSelectedRedValue > 255 ) nSelectedRedValue    = 255;
                    if ( nSelectedGreenValue > 255 ) nSelectedGreenValue = 255;
                    if ( nSelectedBlueValue > 255 ) nSelectedBlueValue  = 255;
                    
                    m_avLight[nCntY][nCntX].color = al_map_rgb(cast(ubyte)nSelectedRedValue, cast(ubyte)nSelectedGreenValue, cast(ubyte)nSelectedBlueValue);
                }
            }
        }
        
        return true;
    }
    
    public bool SetLightRadiusWithElipse(int XLightPos, int YLightPos, int nXRadius, int nYRadius, 
                                         int nRedValue, int nGreenValue, int nBlueValue, 
                                         float rDarkRate = 1.0f)
    {
        int nX, nY;
        GetScreenPosToLightTile(XLightPos, YLightPos, nX, nY);
        
        int nStartTileX         = nX - nXRadius;
        int nStartTileY         = nY - nYRadius;
        int nEndTileX           = nX + nXRadius + 1;
        int nEndTileY           = nY + nYRadius + 1;
        
        real rXRadius = (nXRadius * TILE_WIDTH);
        real rYRadius = (nYRadius * TILE_HEIGHT);
        int nRadius = 0;
        
        if ( rXRadius > rYRadius )
        {
            nRadius = cast(int)rXRadius;
        }
        else
        {
            nRadius = cast(int)rYRadius;
        }
        
        real rLengthCnt = 0f;
        int nSelectedRedValue = 0; 
        int nSelectedGreenValue = 0; 
        int nSelectedBlueValue = 0;
        
        ubyte rr, gg, bb, aa;
        
        if ( nStartTileX < 0 )                  nStartTileX = 0;
        if ( nStartTileY < 0 )                  nStartTileY = 0;
        if ( nEndTileX > NUM_CNTX_LIGHTTILE+1 ) nEndTileX = NUM_CNTX_LIGHTTILE+1;
        if ( nEndTileY > NUM_CNTY_LIGHTTILE+1 ) nEndTileY = NUM_CNTY_LIGHTTILE+1;
        
        
        for ( int nCntY = nStartTileY; nCntY < nEndTileY; nCntY++ )
        {
            for ( int nCntX = nStartTileX; nCntX < nEndTileX; nCntX++ )
            {
                if ( nCntX < NUM_CNTX_LIGHTTILE && nCntY < NUM_CNTY_LIGHTTILE )
                {
                    auto pwIndex = &m_pwLightIndices[(nCntX+nCntY*(NUM_CNTX_LIGHTTILE))*6];
                    // Æ÷ÀÎÆ®Á¡À» Æ÷ÇÔÇÏÁö ¾Ê´Â ÁÂÇÏ. Æ÷ÀÎÆ®Á¡À» Æ÷ÇÔÇÏÁö ¾Ê´Â ¿ì»ó.
                    if ( (nCntX > nX && nCntY < nY) || (nCntX < nX && nCntY > nY) )
                    {
                        *pwIndex++ = (nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY);
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY);  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));
                        *pwIndex++ = (nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY);
                        *pwIndex++ = nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));
                    }
                    else
                    {
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY);  
                        *pwIndex++ = nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));
                        *pwIndex++ = nCntX+((NUM_CNTX_LIGHTTILE+1)*(nCntY+1));  
                        *pwIndex++ = (nCntX+1)+((NUM_CNTX_LIGHTTILE+1)*nCntY);  
                        *pwIndex++ = (nCntX)+((NUM_CNTX_LIGHTTILE+1)*nCntY);
                    }
                }
                
                /*          RECT rc;
                SetRect(&rc,
                       (nCntX*TILE_WIDTH+TILE_START_XPOS),
                       (nCntY*TILE_HEIGHT+TILE_START_YPOS),
                       (nCntX*TILE_WIDTH+TILE_START_XPOS+TILE_WIDTH),
                       (nCntY*TILE_HEIGHT+TILE_START_YPOS+TILE_HEIGHT));
                g_xMainWnd.DrawWithGDI(rc, NULL, RGB(0, 255, 255), 1);
    */
                
                int nCurrCntXRate, nCurrCntYRate;
                if ( rXRadius > rYRadius )      
                {
                    nCurrCntXRate = nCntX;
                    nCurrCntYRate = nCntY + cast(int)((nCntY-nY)*cast(float)(rXRadius/rYRadius-1.0f));
                }
                else if ( rXRadius < rYRadius )                                     
                {
                    nCurrCntXRate = nCntX + cast(int)((nCntX-nX)*cast(float)(rYRadius/rXRadius-1.0f));
                    nCurrCntYRate = nCntY;
                }
                else
                {
                    nCurrCntXRate = nCntX;
                    nCurrCntYRate = nCntY;
                }

                real xx = XLightPos-(nCntX*TILE_WIDTH+TILE_START_XPOS);
                real yy = YLightPos-(nCntY*TILE_HEIGHT+TILE_START_YPOS);
                rLengthCnt = sqrt(xx * xx + yy * yy);
                //rLengthCnt  = sqrt(cast(double)(XLightPos-(nCurrCntXRate*TILE_WIDTH+TILE_START_XPOS))*(XLightPos-(nCurrCntXRate*TILE_WIDTH+TILE_START_XPOS)) + 
                //                   (YLightPos-(nCurrCntYRate*TILE_HEIGHT+TILE_START_YPOS))*(YLightPos-(nCurrCntYRate*TILE_HEIGHT+TILE_START_YPOS)));
                
                /*          SetRect(&rc,
                       (XLightPos),
                       (YLightPos),
                       (XLightPos+5),
                       (YLightPos+5));
                g_xMainWnd.DrawWithGDI(rc, NULL, RGB(255, 0, 0), 1);
    */
                if ( rLengthCnt <= nRadius )
                {
                    nSelectedRedValue   = nRedValue - iround((nRedValue * rLengthCnt * rDarkRate) / nRadius);
                    nSelectedGreenValue = nGreenValue - iround((nGreenValue * rLengthCnt * rDarkRate) / nRadius);
                    nSelectedBlueValue  = nBlueValue - iround((nBlueValue * rLengthCnt * rDarkRate) / nRadius);
                    /*          SetRect(&rc,
                           (nCntX*TILE_WIDTH+TILE_START_XPOS),
                           (nCntY*TILE_HEIGHT+TILE_START_YPOS),
                           (nCntX*TILE_WIDTH+TILE_START_XPOS+2),
                           (nCntY*TILE_HEIGHT+TILE_START_YPOS+2));
                    g_xMainWnd.DrawWithGDI(rc, NULL, RGB(255, 0, 0), 1);
        */          }
                else
                {
                    nSelectedRedValue = nSelectedGreenValue = nSelectedBlueValue = 0;
                }
                
                if ( nSelectedRedValue   < 0 ) nSelectedRedValue    = 0;
                if ( nSelectedGreenValue < 0 ) nSelectedGreenValue = 0;
                if ( nSelectedBlueValue  < 0 ) nSelectedBlueValue   = 0;
                
                al_unmap_rgba(m_avLight[nCntY][nCntX].color, &rr, &gg, &bb, &aa);

                if ( rr == 0 && gg == 0 && bb == 0 )
                    m_avLight[nCntY][nCntX].color = al_map_rgba(cast(ubyte)nSelectedRedValue, cast(ubyte)nSelectedGreenValue, cast(ubyte)nSelectedBlueValue, 0);
                else
                {
                    if ( nSelectedRedValue < rr )
                        nSelectedRedValue = rr;
                    if ( nSelectedGreenValue < gg )
                        nSelectedGreenValue = gg;
                    if ( nSelectedBlueValue < bb )
                        nSelectedBlueValue = bb;
                    
                    m_avLight[nCntY][nCntX].color = al_map_rgba(cast(ubyte)nSelectedRedValue, cast(ubyte)nSelectedGreenValue, cast(ubyte)nSelectedBlueValue, 0);
                }
            }
        }
        
        return true;
    }
}